package yogiro.controller;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import yogiro.service.MessageService;
import yogiro.to.MemberTO;
import yogiro.to.MessageTO;
import yogiro.to.messageListTO;
import yogiro.util.Utilities;

public class MessageController extends MultiActionController{
	private MessageService service;
	public MessageController(MessageService service){
		this.service = service;
	}
	
	//client가 message등록
	public ModelAndView registController(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = null;
		String title = request.getParameter("title");
		String writer = request.getParameter("writer");
		String content = request.getParameter("content");
		String messageReciver = request.getParameter("messageReciver");
		try {
			MessageTO mto = service.insertMessage(title, writer, content, messageReciver);
			mto.setMessageContent(Utilities.changeContentForDB(mto.getMessageContent()));
			mto.setMessageWritedate(Utilities.changeDayTimeFormat(mto.getMessageWritedate()));
			mv = new ModelAndView("client_message/show_client","mto",mto);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	//admin 전체 문의목록 보기
	public ModelAndView listController(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = null;
		String p = request.getParameter("page");
	
		int page = 0;
		try {
			if(p!=null&&!p.equals("")){
				page = Integer.parseInt(p);
			}else{
				page = 1;
			}
			messageListTO lto = service.getList(page);
			
			mv = new ModelAndView("admin_message/admin_show","lto",lto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mv;
	}
	
	//client admin에게 답장받은 메시지 보기
	public ModelAndView ClientReplyController(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		ModelAndView mv = null;
		String p = request.getParameter("page");
		MemberTO mto = (MemberTO) session.getAttribute("mto");
		String messageWriter = mto.getMemberId();
		int page = 0;
		try {
			if(p!=null&&!p.equals("")){
				page = Integer.parseInt(p);
			}else{
				page = 1;
			}
			messageListTO lto = service.getClientReplyList(page,messageWriter);
			
			mv = new ModelAndView("client_message/cl_reply_message","lto",lto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	//admin client에게 보낸 답장 전체보기
	public ModelAndView ReplyListController(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = null;
		String p = request.getParameter("page");
		
		int page = 0;
		try {
			if(p!=null&&!p.equals("")){
				page = Integer.parseInt(p);
			}else{
				page = 1;
			}
			messageListTO lto = service.getReplyList(page);
			
			mv = new ModelAndView("admin_message/admin_reply","lto",lto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	//client 자신이 admin에게 보낸 글 전체보기
	public ModelAndView ClientListController(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		ModelAndView mv = null;
		String p = request.getParameter("page");
		MemberTO mto = (MemberTO) session.getAttribute("mto");
		String messageReceiver = mto.getMemberId();
		int page = 0;
		try {
			if(p!=null&&!p.equals("")){
				page = Integer.parseInt(p);
			}else{
				page = 1;
			}
			messageListTO lto = service.getClientList(page,messageReceiver);
			mv = new ModelAndView("client_message/client_message","lto",lto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	//글을 클릭하였을때 보여주는 화면으로 뿌리는 것
	public ModelAndView linkController(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = null;
		String no = request.getParameter("no");
		String page = request.getParameter("page");
		String id = request.getParameter("id");
		int messageNo = Integer.parseInt(no);
		request.setAttribute("page", page);
		try {
			MessageTO mto = service.selectByNoMessage(messageNo);
			mto.setMessageContent(Utilities.changeContentForDB(mto.getMessageContent()));
			if(id.equals("admin")){
				mv = new ModelAndView("admin_message/show_message","mto",mto);
			}else if(id.equals("client")){
				mv = new ModelAndView("client_message/show_client","mto",mto);
			}else if(id.equals("adReply")){
				mv = new ModelAndView("admin_message/reply_show","mto",mto);
			}else if(id.equals("clReply")){
				mv = new ModelAndView("client_message/client_reply_show_message","mto",mto);
			}else if(id.equals("search")){
				mv = new ModelAndView("admin_message/search_show_message","mto",mto);
			}else if(id.equals("replySearch")){
				mv = new ModelAndView("admin_message/reply_show_message","mto",mto);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	//글삭제 하는 구문
	public ModelAndView deleteController(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = null;
		String no = request.getParameter("no");
		String page = request.getParameter("page");
		String name = request.getParameter("name");
		String searchId = request.getParameter("searchId");
		int messageNo = Integer.parseInt(no);
		request.setAttribute("page", page);
		try {
			service.updateSend(messageNo);
			MessageTO mto = service.selectByNoMessage(messageNo);
			if(mto.getSend()==1 && mto.getReceive()==1){
				service.deleteByNo(messageNo);
			}
			if(name.equals("adminReply")){		
				mv = ReplyListController(request, response);
			}else if(name.equals("admin")){
				mv = listController(request, response);
			}else if(name.equals("search")){
				request.setAttribute("searchId", searchId);
				mv = searchByIdController(request, response);
			}else if(name.equals("replySearch")){
				request.setAttribute("searchId", searchId);
				request.setAttribute("id", "replyMessage");
				mv = searchByIdController(request, response);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	//client 글 삭제구문
	public ModelAndView clientDeleteController(HttpServletRequest request, HttpServletResponse response,HttpSession session){
		ModelAndView mv = null;
		String no = request.getParameter("no");
		String page = request.getParameter("page");
		String name = request.getParameter("name");
		int messageNo = Integer.parseInt(no);
		request.setAttribute("page", page);
		
		try {
			service.updateReceive(messageNo);
			MessageTO mto = service.selectByNoMessage(messageNo);
			if(mto.getSend()==1 && mto.getReceive()==1){
				service.deleteByNo(messageNo);
			}
			if(name.equals("clientReply")){
				mv = ClientReplyController(request, response, session);
			}else{
				mv = ClientListController(request, response, session);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	//답변하기 눌렀을때 답변하는 폼으로 가는 구문
	public ModelAndView showController(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = null;
		String page = request.getParameter("page");
		String no = request.getParameter("no");
		int messageNo = Integer.parseInt(no);
		
		try {
			MessageTO mto = service.selectByNoMessage(messageNo);
			request.setAttribute("page", page);
			mv = new ModelAndView("admin_message/reply_message","mto",mto);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return mv;
	}
	
	//메시지 답변 controller
	public ModelAndView replyController(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = null;
		String title = request.getParameter("title");
		String writer = request.getParameter("writer");
		String content = request.getParameter("content");
		String no = request.getParameter("no");
		String messageReceiver = request.getParameter("messageReceiver");
		int messageNo = Integer.parseInt(no);
		try {
			MessageTO mto = service.selectByNoMessage(messageNo);
			mto = service.insertMessageClient(title, writer, content, messageReceiver);
			mto.setMessageContent(Utilities.changeContentForDB(mto.getMessageContent()));
			mv = new ModelAndView("admin_message/show_message","mto",mto);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return mv;
	}
	
	//전체받은쪽지중 아이디로 검색
	public ModelAndView searchByIdController(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = null;
		String searchId = request.getParameter("searchId");
		String id = request.getParameter("id");//받은쪽지함인지 보낸쪽지함인지 구분하는 기준
		String p = request.getParameter("page");
		int page = 0;
		try {
			if(p!=null&&!p.equals("")){
				page = Integer.parseInt(p);
			}else{
				page = 1;
			}
			messageListTO lto = service.getSearchByIdList(page,searchId,id);
			if(id.equals("message")){
				mv = new ModelAndView("admin_message/search_show","lto",lto); 
			}else if(id.equals("replyMessage")){
				mv = new ModelAndView("admin_message/reply_search_show","lto",lto); 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	//전체 client에게 쪽지보내기
	public ModelAndView AllListController(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = null;
		String title = request.getParameter("title");
		String writer = request.getParameter("writer");
		String content = request.getParameter("content");
		try {
			ArrayList<MemberTO> list = service.selectMemberId();
			for(int i=0;i<list.size();i++){
				service.insertMessageClient(title, writer, content,list.get(i).getMemberId());
			}
			mv = listController(request, response);
					
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	//admin에게 쪽지보내기
	public ModelAndView writeMessage(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = null;
		try {
			String memberId = service.selectAdmin();
			mv = new ModelAndView("message/message","memberId",memberId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return mv;
	}
	public ModelAndView allWriteMessage(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView("message/all_message");
		return mv;
	}
}
